package com.lonely.dialect;

import org.apache.calcite.avatica.util.Casing;
import org.apache.calcite.config.NullCollation;
import org.apache.calcite.sql.SqlDialect;
import org.apache.calcite.sql.dialect.MysqlSqlDialect;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * @date: 2022-11-29 14:41
 * @author: ztkj-hzb
 * @description:
 **/
public class Utf8WithMySqlDialect extends MysqlSqlDialect {

    public static final SqlDialect.Context DEFAULT_CONTEXT = SqlDialect.EMPTY_CONTEXT
            .withDatabaseProduct(SqlDialect.DatabaseProduct.MYSQL)
            .withIdentifierQuoteString("`")
            .withDataTypeSystem(MYSQL_TYPE_SYSTEM)
            .withUnquotedCasing(Casing.UNCHANGED)
            .withNullCollation(NullCollation.LOW);

    public static final SqlDialect DEFAULT = new Utf8WithMySqlDialect(DEFAULT_CONTEXT);


    /**
     * Creates a MysqlSqlDialect.
     *
     * @param context
     */
    public Utf8WithMySqlDialect(Context context) {
        super(context);
    }

    /**
     * Appends a string literal to a buffer.
     *
     * @param buf         Buffer
     * @param charsetName Character set name, e.g. "utf16", or null
     * @param val         String value
     */
    @Override
    public void quoteStringLiteral(StringBuilder buf, @Nullable String charsetName,
                                   String val) {
        buf.append(this.literalQuoteString);
        buf.append(val.replace(this.literalEndQuoteString, this.literalEscapedQuote));
        buf.append(this.literalEndQuoteString);
    }
}
